import { PAGE, SIZE } from '@/constant'
import { type TableComponentExposed } from '@/components/table/index.vue'
/**
 *
 * @param tableRef table组件对象
 * @returns
 */
export function useTable() {
  // 初始数据的默认值
  const usePage = ref(PAGE)
  const useSize = ref(SIZE)

  /**
   * ref
   */
  const tableRef = ref<TableComponentExposed | null>(null)

  // 开始loading
  const useTableLoadingStart = () => {
    tableRef.value?.setLoading(true)
  }
  // 结束loading
  const useTableLoadingEnd = () => {
    tableRef.value?.setLoading(false)
  }
  // 获取选中数据
  const useGetCheckedData = () => tableRef.value?.getCheckedRow()
  // 设置总条数
  const useSetTotal = (total: number) => {
    tableRef.value?.totalCount(total)
  }
  // 搜索
  const useTableSearch = () => {
    usePage.value = PAGE
    tableRef.value?.resetPage({ pageIndex: PAGE })
  }
  const useTableReset = () => {
    usePage.value = PAGE
    useSize.value = SIZE
    tableRef.value?.resetPage({ pageIndex: PAGE, pageSize: SIZE })
  }

  return {
    tableRef,
    usePage,
    useSize,
    useTableLoadingStart,
    useTableLoadingEnd,
    useGetCheckedData,
    useTableSearch,
    useTableReset,
    useSetTotal,
  }
}
